package pe.com.hermes.hermetic.modelo.daoimpl;

import pe.com.hermes.hermetic.beans.TiposBean;

import pe.com.hermes.hermetic.modelo.dao.MonedasDAO;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.stereotype.Repository;

@Repository("monedasDAO")
public class MonedasDAOImpl  implements MonedasDAO{
    @Autowired
    JdbcTemplate jdbcTemplate;
    protected Logger logger = Logger.getLogger(this.getClass());

    
    public List selectAllMonedas() {
        AllMonedas sproc = new AllMonedas(jdbcTemplate.getDataSource());
        Map map = sproc.execute();
        return (List)map.get("s_list");
    }
    public List getUnidMonByCnta(String cod_clie, String cod_sucu_htb, String cod_cnta){
        MonedasByCnta sproc = new MonedasByCnta(jdbcTemplate.getDataSource());
        Map map = sproc.execute(cod_clie,cod_sucu_htb,cod_cnta);
        logger.info(map.get("S_MENS"));
        return (List)map.get("S_LIST");
    }
    public String descMonedaById(String cod_deno){
        MonedaById sproc = new MonedaById(jdbcTemplate.getDataSource());
        Map map = sproc.execute(cod_deno);
        logger.error((String)map.get("s_mens"));
        return (String)map.get("s_des_deno");
    }
    
    public class AllMonedas extends StoredProcedure {
        private static final String SPROC_NAME = "APERTURA.HWPG_TABL.HWPR_LIST_MONE";
        
        private AllMonedas(DataSource dataSource){
            super(dataSource,SPROC_NAME);
            declareParameter(new SqlOutParameter("s_list", OracleTypes.CURSOR, new MonedasMapper()));
            compile();
        }
            public Map execute() {
                        Map inputs = new HashMap();
                        return super. execute(inputs);
            }
            
            protected class MonedasMapper implements RowMapper{

            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                TiposBean monedaBean = new TiposBean();
                monedaBean.setCodigo(rs.getString(1));
                monedaBean.setDescripcion(rs.getString(2));
                return monedaBean;
            }
        }
    }
    
    public class MonedasByCnta extends StoredProcedure {
        private static final String SPROC_NAME = "APERTURA.HWPG_TABL.HWPR_LIST_UNID_MONE_CNTA";
        
        private MonedasByCnta(DataSource dataSource){
            super(dataSource,SPROC_NAME);
            declareParameter(new SqlParameter("P_COD_CLIE",OracleTypes.VARCHAR) );
            declareParameter(new SqlParameter("P_COD_SUCU_HTB",OracleTypes.VARCHAR) );
            declareParameter(new SqlParameter("P_COD_CNTA",OracleTypes.VARCHAR) );
            declareParameter(new SqlOutParameter("S_MENS", OracleTypes.VARCHAR) );
            declareParameter(new SqlOutParameter("S_LIST", OracleTypes.CURSOR, new MonedasMapper()));
            compile();
        }
            public Map execute(String cod_clie, String cod_sucu_htb, String cod_cnta) {
                        Map inputs = new HashMap();
                        inputs.put("P_COD_CLIE",cod_clie);
                        inputs.put("P_COD_SUCU_HTB",cod_sucu_htb);
                        inputs.put("P_COD_CNTA",cod_cnta);
                        return super. execute(inputs);
            }
            
            protected class MonedasMapper implements RowMapper{

            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                TiposBean monedaBean = new TiposBean();
                monedaBean.setCodigo(rs.getString(1));
                monedaBean.setDescripcion(rs.getString(2));
                monedaBean.setDescripcion2(rs.getString(3));
                monedaBean.setDescripcion3(rs.getString(3));
                return monedaBean;
            }
        }
    }

    private class MonedaById extends StoredProcedure{
        private static final String SPROC_NAME = "APERTURA.HWPG_TABL.HWPR_RETU_DESC_MONE";
        
        private MonedaById(DataSource dataSource) {
            super(dataSource,SPROC_NAME);
        declareParameter(new SqlParameter("p_cod_deno",OracleTypes.VARCHAR) );
        declareParameter(new SqlOutParameter("s_mens",OracleTypes.VARCHAR) );
        declareParameter(new SqlOutParameter("s_des_deno",OracleTypes.VARCHAR) );
        compile();
        }

        private Map execute(String cod_deno) {
            Map parameterMap = new HashMap();
            parameterMap.put("p_cod_deno",cod_deno);
            return super.execute(parameterMap);
        }
    }
}
